<?php

function applyXSLT($data, $xsltPath, $params = array()) {
	if($data == "") return "";
	
	$data = utf8_encode($data);

	$xp = new XsltProcessor();

	$xsl = new DomDocument;
	$xsl->load($xsltPath);
    
	$xp->setParameter('', $params);
	
	$xp->importStylesheet($xsl);
	
	$xml_doc = new DomDocument;
	$xml_doc->loadXML($data);
  
	if ($output = $xp->transformToXML($xml_doc)) {
      return $output;
	} else {
      trigger_error('XSL transformation failed.', E_USER_ERROR);
	} 
}

class DB {
	public $conn;
	
	function __construct($dbServer, $dbName) {
       $this->conn = sqlsrv_connect($dbServer, array("Database" => $dbName));
	   
	   if($this->conn === false ) { die( print_r( sqlsrv_errors(), true)); }
   }
	
	public function query($query, $close = false) {
		$stmt = sqlsrv_query($this->conn, $query);
	
		if( $stmt === false ) { die( print_r( sqlsrv_errors(), true)); }
		
		$row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC);
		
		sqlsrv_free_stmt( $stmt);
		
		if($close) { $this->close(); }
		
		return $row[0];
	}
	
	public function close() {
		sqlsrv_close($this->conn);
	}	
}